<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryParamsRef" :inline="true" :model="queryParams" label-width="100px">
        <el-form-item label="起止时间" prop="startTime">
          <el-date-picker v-model="dateRange" :unlink-panels="true" end-placeholder="结束日期"
                          range-separator="截止" start-placeholder="开始日期" type="daterange"
                          @change="startTimeChange"></el-date-picker>
        </el-form-item>
        <el-form-item label="对账状态" prop="stmt_flag">
          <el-select v-model="queryParams.stmt_flag" placeholder="请选择对账状态" class="width-120" clearable>
            <el-option
                v-for="item in uploadFlagDict"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <div class="mr20" style="float: right">
          <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery"/>
        </div>
      </el-form>
    </div>


    <el-table v-adaptive :border="true" :data="orgCostToTalList" :header-cell-style="$headerCellStyle" :stripe='true'
              highlight-current-row>
      <el-table-column label="险种类型" :align="'center'" prop="insutype">
        <template #default="scope">
          <template v-for="(item, index) in dictDataMap['insurance_type']">
            <span v-if="item.dictValue === scope.row.insutype">{{ item.dictLabel }}</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="清算类别" :align="'center'" prop="clr_type">
        <template #default="scope">
          <template v-for="(item, index) in dictDataMap['clr_type']">
            <span v-if="item.dictValue === scope.row.clr_type">{{ item.dictLabel }}</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="医疗费总额" :align="'center'" prop="medfee_sumamt"/>
      <el-table-column label="基金支付总额" :align="'center'" prop="fund_pay_sumamt"/>
      <el-table-column label="个人账户金额" :align="'center'" prop="acct_pay"/>
      <el-table-column label="结算笔数" :align="'center'" prop="fixmedins_setl_cnt"/>
      <el-table-column :align="'center'" class-name="small-padding fixed-width" label="操作" width="240" v-if="queryParams.stmt_flag == '1'" >
        <template #default="scope">
          <el-button type="success" @click="handleUpdate(scope.row)"><template #icon>
            <i-ep-edit />
          </template>对账
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum"
                :total="total" @pagination="getList" />

  </div>
</template>

<script lang="ts" setup>
import {useDictStoreHook} from "@/store/modules/dict.ts";
import {getOrgCostTotalList, orgCostTotal} from "@/api/msip/reconciliation.ts";
import {ElMessage} from "element-plus";

import {useUserStoreHook} from "@/store/modules/user.ts";

const useUserStore = useUserStoreHook();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;

const {proxy} = getCurrentInstance() as any;
const uploadFlagDict = [
  {label: '未对账', value: '1'},
  {label: '已对账', value: '0'},
];
const date = proxy.$parseTime(new Date(), "{y}-{m}-{d} ");
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth();
// 创建一个表示当月第一天的Date对象
const begin = proxy.$parseTime(new Date(year, month, 1), "{y}-{m}-{d} ");
const end = proxy.$parseTime(new Date(), "{y}-{m}-{d} ");
const dateRange = ref([begin + "00:00:00", end + "23:59:59"]);
const dictStore = useDictStoreHook();
let dictDataMap = ref(dictStore.isTypeGetData(
    'insurance_type','clr_type'
))
const orgCostToTalList = ref<any>([])
const total = ref(0)
const queryParams = ref<any>({
  pageNum: 1,
  pageSize: 10,
  stmt_flag: "1",
  stmt_begndate: "",
  stmt_enddate: "",
  org_id: userInfo.orgId,
})

function startTimeChange(e) {
  if (e) {
    dateRange.value = [proxy.$parseTime(e[0], "{y}-{m}-{d} 00:00:00"), proxy.$parseTime(e[1], "{y}-{m}-{d} 23:59:59")]
  }
}

function getList() {
  if (!dateRange.value || dateRange.value.length === 0) {
    ElMessage.error("起止时间不能为空");
    return;
  }
  queryParams.value.stmt_begndate = dateRange.value[0];
  queryParams.value.stmt_enddate = dateRange.value[1];
  getOrgCostTotalList(queryParams.value).then((response: any) => {
    orgCostToTalList.value = response.rows;
    total.value = response.total;
  });
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList()
}

/** 对账按钮操作 */
function handleUpdate(row) {
  let param = {
    stmt_begndate: dateRange.value[0],
    stmt_enddate: dateRange.value[1],
    insutype: row.insutype,
    clr_type: row.clr_type,
    medfee_sumamt: row.medfee_sumamt,
    fund_pay_sumamt: row.fund_pay_sumamt,
    acct_pay: row.acct_pay,
    fixmedins_setl_cnt: row.fixmedins_setl_cnt,
    org_id: userInfo.orgId,
  };
  orgCostTotal(param).then((res: any) => {
    if (res && res.code === 200 ) {
      debugger
      const output = res.data.output;
      if (output.stmt_rslt == '0') {
        ElMessage.success("对账成功");
      } else {
        return ElMessage.error(output.stmt_rslt_dscr)
      }
    }else {
      return ElMessage.error(res.msg)
    }
  });
}

function resetQuery() {
  // 初始化按钮
  dateRange.value = [proxy.$parseTime(new Date(), "{y}-{m}-{d} 00:00:00"), proxy.$parseTime(new Date(), "{y}-{m}-{d} 23:59:59")]
  getList()
}
</script>

<script lang="ts">
export default {
  name: "ReconciliationAssemble"
}
</script>
<style scoped>

</style>
